﻿using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using Microsoft.Commerce.Proxy.PaymentInstrumentService;

namespace TestWebApplication.CommercePaymentInstrument.APIs
{
    public class CommerceAddPaymentInstrumentApi : CommercePaymentInstrumentApi
    {
        private AddPaymentInstrumentInput input;
        private AddPaymentInstrumentOutput output;

        private CallerInfo callerInfo;
        private PaymentInstrument piInfo;

        public CommerceAddPaymentInstrumentApi(BasicHttpBinding binding, EndpointAddress address, X509Certificate2 clientCert, PaymentInstrument piInfo, CallerInfo callerInfo)
            : base(binding, address, clientCert)
        {
            this.callerInfo = callerInfo;
            this.piInfo = piInfo;
        }

        public CommerceAddPaymentInstrumentResult AddPIResult { get; private set; }

        protected override AbstractResponse ExecuteInternal(PaymentInstrumentServiceChannel channel)
        {
            this.output = channel.AddPaymentInstrument(this.input);

            return output;
        }

        public override void PrepareRequestData()
        {
            this.input = new AddPaymentInstrumentInput()
            {
                APIContext = new APIContext()
                {
                    TrackingGuid = this.trackingGuid,
                },

                CallerInfo = this.callerInfo,
                PaymentInstrument = this.piInfo,
            };

            return;
        }

        public override void PrepareResponseData()
        {
            this.AddPIResult = new CommerceAddPaymentInstrumentResult()
            {
                NextPhase = this.output.NextPhase,
                PIInfo = this.output.PaymentInstrument,
            };
        }
    }
}
